package api

import (
	"aglaia/global"
	"aglaia/internal/service"
	"aglaia/pkg/app"
	"aglaia/pkg/errcode"
	"github.com/gin-gonic/gin"
	"strconv"
)

func Articles(c *gin.Context) {
	response := app.NewResponse(c)
	page := app.GetPage(c)
	pageSize := app.GetPageSize(c)

	svc := service.New(c)
	articles, total, err := svc.Articles(page, pageSize)
	if err != nil {
		global.Logger.Infof("svc.Articles err: %v\n", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}

	response.ToResponseList(articles, total)
}

func QueryArticleById(c *gin.Context) {
	response := app.NewResponse(c)

	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		global.Logger.Infof("c.Query err: %v\n", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}

	svc := service.New(c)
	article, err := svc.QueryArticleById(uint(id))
	if err != nil {
		global.Logger.Infof("svc.QueryArticleById err: %v\n", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}

	response.ToResponse(article)
}

func SearchArticles(c *gin.Context) {
	response := app.NewResponse(c)

	data := c.Query("data")
	if data == "" {
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}

	svc := service.New(c)
	article, err := svc.SearchArticle(data)
	if err != nil {
		global.Logger.Infof("svc.QueryArticleById err: %v\n", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}

	response.ToResponse(article)
}

func CreateArticle(c *gin.Context) {
	param := service.CreateArticleRequest{}
	response := app.NewResponse(c)
	if err := response.Bind(&param); err != nil {
		global.Logger.Infof("response.Bind err: %v\n", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}

	svc := service.New(c)
	authId, err := response.AuthId()
	if err != nil {
		global.Logger.Infof("response.AuthId err: %v\n", err)
		response.ToErrorResponse(errcode.UnauthorizedTokenError)
		return
	}
	if err := svc.CreateArticle(authId, param); err != nil {
		global.Logger.Infof("svc.CreateArticle err: %v\n", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}

	response.ToResponse(nil)
}

func UpdateArticle(c *gin.Context) {
	param := service.UpdateArticleRequest{}
	response := app.NewResponse(c)
	if err := response.Bind(&param); err != nil {
		global.Logger.Infof("response.Bind err: %v\n", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	svc := service.New(c)
	if err := svc.UpdateArticle(param); err != nil {
		global.Logger.Infof("svc.UpdateArticle err: %v\n", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}

	response.ToResponse(nil)
}

func DeleteArticle(c *gin.Context) {
	param := service.DeleteArticleRequest{}
	response := app.NewResponse(c)
	if err := response.Bind(&param); err != nil {
		global.Logger.Infof("response.Bind err: %v\n", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	svc := service.New(c)
	if err := svc.DeleteArticle(param); err != nil {
		global.Logger.Infof("svc.DeleteArticle err: %v\n", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}

	response.ToResponse(nil)
}
